/**
 * 
 */
package kr.co.insoft.core.service.menus.impl;

import java.util.HashSet;

import kr.co.insoft.core.service.menus.MenuService;
import kr.co.insoft.framework.config.MenuEntityModel;
import kr.co.insoft.framework.config.Menus;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Service
public class MenuServiceImpl implements MenuService {

	@Override
	public MenuEntityModel getMenus() throws ServiceException {
		HashSet<String> enabledMenus = null;
		MenuEntityModel result = new MenuEntityModel();
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			Assert.notNull(auths);
			enabledMenus = Menus.parseEnabledMenuIds(auths.getAuthorities());
			Assert.isTrue(enabledMenus.size() > 0);
		} catch (IllegalArgumentException e) {
			result.setMenus(Menus.ROOT);
			return result;
		}
		try {
			return (new MenuCompute(enabledMenus)).getTree();
		} catch (IllegalArgumentException e) {
			throw new ServiceException(ExceptionCode.NOT_FOUND_EXCEPTION
					+ "##### Don't has Authoritues!!!");
		}
	}
}
